#
# Imports
#
import sys
import urllib
import xbmc
import xbmcgui
import xbmcplugin
from xml.dom import minidom

#
# Main class
#
class Main:
    def __init__( self ):
		#
		# Plugins (All)
		#
		listitem = xbmcgui.ListItem( xbmc.getLocalizedString(30201), iconImage="DefaultFolder.png" )
		xbmcplugin.addDirectoryItem( handle = int(sys.argv[ 1] ), url = sys.argv[ 0 ] + '?action=script-list&category=All', listitem=listitem, isFolder=True)
		
		#
		# Plugins (categories)
		#
		
		# Get page (HTML)...
		usock = urllib.urlopen( "http://www.xbmczone.com/installer/addon_categories.asp?type=Script" )
		dom = minidom.parse( usock )
		
		for category in dom.getElementsByTagName('Category') :
			for child in category.childNodes:
				if child.localName == "Type" :
					type = child.childNodes[0].data
				elif child.localName == "Name" :
					category = child.childNodes[0].data

			# Add entry...
			listitem = xbmcgui.ListItem( category.replace("Scripts", "").replace("Script", ""), iconImage="DefaultFolder.png" )
			xbmcplugin.addDirectoryItem( handle = int(sys.argv[ 1 ]), url = sys.argv[ 0 ] + '?action=script-list&category=%s' % urllib.quote( category ), listitem=listitem, isFolder=True)

		# Disable sorting...
		xbmcplugin.addSortMethod( handle=int( sys.argv[ 1 ] ), sortMethod=xbmcplugin.SORT_METHOD_NONE )
	
		# Label (top-right) = "Plugins"
		xbmcplugin.setPluginCategory( handle=int( sys.argv[ 1 ] ), category=xbmc.getLocalizedString(30002) )

		# End of list...
		xbmcplugin.endOfDirectory( handle=int( sys.argv[ 1 ] ), succeeded=True )        
